
// DlgTaskPropEdit.cpp : implementation file
//

#include "stdafx.h"
#include "mxgtd.h"
#include "DlgTaskPropEdit.h"
#include "afxdialogex.h"
#include "EasySize.h"
#include "../core/GtdTime.h"
#include "../core/GtdDOMItem.h"


// CDlgTaskPropEdit dialog

IMPLEMENT_DYNAMIC(CDlgTaskPropEdit, CDialogEx)

CDlgTaskPropEdit::CDlgTaskPropEdit(GtdTaskEntry*pEntry, CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgTaskPropEdit::IDD, pParent)
{
	m_pTaskEntry = pEntry;
	m_bTimeF = m_bTimeT = TRUE;
}

CDlgTaskPropEdit::~CDlgTaskPropEdit()
{
}



void CDlgTaskPropEdit::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_DATETIMEPICKER_DATAF, m_ctrlDateF);
	DDX_Control(pDX, IDC_DATETIMEPICKER_TIMEF, m_ctrlTimeF);
	DDX_Control(pDX, IDC_DATETIMEPICKER_DATAT, m_ctrlDateT);
	DDX_Control(pDX, IDC_DATETIMEPICKER_TIMET, m_ctrlTimeT);
	DDX_Text(pDX, IDC_EDIT_NAME, m_pTaskEntry->msName);
	DDX_Check(pDX, IDC_CHECKF, m_bTimeF);
	DDX_Check(pDX, IDC_CHECKT, m_bTimeT);
	DDX_Control(pDX, IDC_LIST_CHECKLIST, m_ctrlCheckList);
	DDX_Control(pDX, IDC_COMBO_SELCONTEXT, m_combSelContext);
}


BEGIN_MESSAGE_MAP(CDlgTaskPropEdit, CDialogEx)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_CHECKF, &CDlgTaskPropEdit::OnBnClickedCheckf)
	ON_BN_CLICKED(IDC_CHECKT, &CDlgTaskPropEdit::OnBnClickedCheckt)	
END_MESSAGE_MAP()


// CDlgTaskPropEdit message handlers


//EASYSIZE(control,left,top,right,bottom,options)
BEGIN_EASYSIZE_MAP(CDlgTaskPropEdit)
	EASYSIZE(IDC_EDIT_NAME,ES_BORDER,ES_BORDER,ES_BORDER,ES_KEEPSIZE,0)
	EASYSIZE(IDC_LIST_CHECKLIST,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,0)
	EASYSIZE(IDOK,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,ES_BORDER,0)
	EASYSIZE(IDCANCEL,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,ES_BORDER,0)
	EASYSIZE(IDC_DATETIMEPICKER_DATAF,ES_BORDER,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,0)
	EASYSIZE(IDC_DATETIMEPICKER_TIMEF,ES_BORDER,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,0)
	EASYSIZE(IDC_DATETIMEPICKER_DATAT,ES_BORDER,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,0)
	EASYSIZE(IDC_DATETIMEPICKER_TIMET,ES_BORDER,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,0)
	EASYSIZE(IDC_CHECKF,ES_BORDER,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,0)
	EASYSIZE(IDC_CHECKT,ES_BORDER,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,0)	
END_EASYSIZE_MAP

void CDlgTaskPropEdit::InitComboContext()
{
	m_combSelContext.ResetContent();
	GtdDOM& dom = theApp.getGTDDOM();

	GuidList ctxlist = dom.GetContextList();
	for(GuidList::const_iterator itr = ctxlist.begin(); itr!=ctxlist.end(); ++itr)
	{
		if( GtdDOMItem* pItem = dom.getItem(*itr) )
		{
			if(GtdContext* pcontext = pItem->toContext() )
			{
				int idx = m_combSelContext.AddString(pcontext->msName);
				bool bCheck = m_pTaskEntry->hasContext(pcontext->m_id);
				m_combSelContext.SetCheck(idx,bCheck);
			}
		}		
	}
}

void CDlgTaskPropEdit::OnOK()
{
	
	if(GetFocus()==&m_ctrlCheckList)
		return ;

	UpdateData(TRUE);
	if(m_pTaskEntry->msName.IsEmpty())
	{
		MessageBox(_T("Input Task Name"));
		return;
	}

	//get time From
	if(m_bTimeF)
	{  
		CTime dateF,timeF;
		m_ctrlDateF.GetTime(dateF); 
		m_ctrlTimeF.GetTime(timeF);
		m_pTaskEntry->mtFrom = CTime(dateF.GetYear(),dateF.GetMonth(),dateF.GetDay(),timeF.GetHour(),timeF.GetMinute(),0);
	}
	else
	{
		m_pTaskEntry->mtFrom = GtdTime::NEG_INF;
	}
	//get time to
	if(m_bTimeT)
	{
		CTime dateT,timeT;
		m_ctrlDateT.GetTime(dateT);
		m_ctrlTimeT.GetTime(timeT);
		m_pTaskEntry->mtTo = CTime(dateT.GetYear(),dateT.GetMonth(),dateT.GetDay(),timeT.GetHour(),timeT.GetMinute(),0);
	}
	else{
		m_pTaskEntry->mtTo = GtdTime::INF;
	}		
	
	//get context
	GuidList ctxlist = theApp.getGTDDOM().GetContextList();
	GuidList selctx;
	for(int i=0;i<m_combSelContext.GetCount();i++)
	{
		if(m_combSelContext.GetCheck(i))
		{
			selctx.push_back(ctxlist.at(i));
		}
	}
	m_pTaskEntry->mCtxlist = selctx;

	__super::OnOK();
}

class CCheckListCtrlItemEditImpl : public ListCtrlItemImpl
{
public:
	CCheckListCtrlItemEditImpl(GtdTaskEntry* pEntry)
	{
		mpEntry = pEntry;		
		pEdit = NULL;
	}

	virtual void OnSetToCtrl(CGTDListCtrl& ctrl)
	{	
		InitCtrl(ctrl);
		loadCtrlDisplay(ctrl);
	}

	void loadCtrlDisplay(CGTDListCtrl& ctrl)
	{
		int destCont = mpEntry->mCheckList.size()+1;
		int count = ctrl.EnLargeItemCount(destCont);
		setCtrlDisp(ctrl);
	}

	void setCtrlDisp(CGTDListCtrl& ctrl)
	{
		ASSERT(ctrl.GetItemCount()==mpEntry->mCheckList.size()+1);
		size_t i=0;
		for(;i<mpEntry->mCheckList.size();i++)
		{	
			CString s;
			s.Format(_T("%d"),i+1);		
			ctrl.SetItemText(i,0, s);
			ctrl.SetItemText(i,1,mpEntry->mCheckList.at(i)->msName);

		}	
		ctrl.SetItemText(i,1,_T(""));
	}
	void InitCtrl(CGTDListCtrl& ctrl)
	{
		ctrl.InitStyle();

		LV_COLUMN lvc;

		CRect rect;
		ctrl.GetClientRect(&rect);

		//time range
		{ 
			lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCFMT_CENTER;
			lvc.pszText = _T("No.");
			lvc.cx = 40;
			//lvc.fmt = LVCFMT_CENTER;
			//lvc.csList = &strList;
			ctrl.InsertColumn(0, &lvc);
		}	
		{//distribution		
			lvc.cx = rect.Width()-20;
			lvc.pszText = _T("Text");
			//lvc.fmt = LVCFMT_DROPDOWN;
			ctrl.InsertColumn(1, &lvc);
			//m_listTaxiAndIterate.SetInputTerminal(interminal);
		}
	}
	void DeleteItem(CGTDListCtrl& ctrl, int index)
	{
		//mpEnt
		ctrl.DeleteItem(index);
		if(index<ctrl.GetItemCount())
			ctrl.SelectItem(index);
		loadCtrlDisplay(ctrl);
	}
	virtual void OnDoneEdit(CGTDListCtrl& ctrl, int index, int column )
	{		
		int nCount = mpEntry->mCheckList.size();
		if(column==1 && pEdit)
		{
			CString sRet;
			pEdit->GetWindowText(sRet);
			if(sRet.IsEmpty())
			{
				if(nCount<=index)
				{
									
				}	
				else
				{
					return DeleteItem(ctrl,index);
				}
			}
			else
			{
				if(nCount<=index)
				{
					TaskEntryCheckItem* pItem = mpEntry->getDom()->allocateDOMItem<TaskEntryCheckItem>();
					pItem->msName = sRet;
					mpEntry->AddCheckItem(pItem);		
					loadCtrlDisplay(ctrl);
					ctrl.SelectItem(index+1);
					return;
				}	
				else
				{
					mpEntry->mCheckList.at(index)->msName = sRet;		
					loadCtrlDisplay(ctrl);
					return;
				}
			}
			
		}
	}	

	virtual void OnChar(CGTDListCtrl& ctrl, UINT nChar, UINT nRepCnt, UINT nFlags)
	{
		if(nChar == VK_RETURN)
		{
			int index;
			if(ctrl.GetCurSelect(index))
			{				
				DblClickItem(ctrl,index,1);
			}
		}	
		if(nChar == VK_DELETE)
		{
			int index;
			if(ctrl.GetCurSelect(index))
			{				
				DeleteItem(ctrl,index);
			}
		}
	}

	virtual void DblClickItem(CGTDListCtrl& ctrl, int index, int column)
	{
		if(column==1)
		{
			pEdit = ShowInPlaceEdit(ctrl,index,column, ctrl.GetItemText(index,column));
		}
	}
protected:
	GtdTaskEntry* mpEntry;	
	CEdit* pEdit;
};


void CDlgTaskPropEdit::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	UPDATE_EASYSIZE;
	// TODO: Add your message handler code here
}


BOOL CDlgTaskPropEdit::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	m_ctrlCheckList.setImpl(new CCheckListCtrlItemEditImpl(m_pTaskEntry) );
	InitComboContext();
	// TODO:  Add extra initialization here
	INIT_EASYSIZE;

	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CDlgTaskPropEdit::OnBnClickedCheckf()
{
	// TODO: Add your control notification handler code here
	if( CButton* pWnd = (CButton*) GetDlgItem(IDC_CHECKF))
	{	
		CWnd* pDataF = GetDlgItem(IDC_DATETIMEPICKER_DATAF);
		CWnd* pTimeF = GetDlgItem(IDC_DATETIMEPICKER_TIMEF);
		if( !pWnd->GetCheck() )
		{
			pDataF->EnableWindow(FALSE);
			pTimeF->EnableWindow(FALSE);
		}
		else
		{
			pDataF->EnableWindow(TRUE);
			pTimeF->EnableWindow(TRUE);
		}
	}
}

void CDlgTaskPropEdit::OnBnClickedCheckt()
{
	// TODO: Add your control notification handler code here
	if( CButton* pWnd = (CButton*) GetDlgItem(IDC_CHECKT))
	{	
		CWnd* pData = GetDlgItem(IDC_DATETIMEPICKER_DATAT);
		CWnd* pTime = GetDlgItem(IDC_DATETIMEPICKER_TIMET);
		if( !pWnd->GetCheck() )
		{
			pData->EnableWindow(FALSE);
			pTime->EnableWindow(FALSE);
		}
		else
		{
			pData->EnableWindow(TRUE);
			pTime->EnableWindow(TRUE);
		}
	}
}


